<template>
  <div>
    <p class="pinformation">搜索条件</p>
    <el-form label-width="80" ref="queryRef" :model="queryParams">
      <el-row>
        <el-col :span="3">
          <el-form-item label="方案编号：" prop="projectCode">
            <el-input maxlength="100" clearable placeholder="请输入方案编号" v-model.trim="queryParams.projectCode"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="检测方法：" prop="methodCode">
            <el-input maxlength="100" clearable placeholder="请输入方法编号或名称" v-model.trim="queryParams.methodCode"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="检测项目：" prop="jcxmName">
            <el-input maxlength="50" clearable placeholder="请输入检测项目" v-model.trim="queryParams.jcxmName"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="填报时间：">
            <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="模板名称：" prop="tableName">
            <el-input maxlength="100" clearable placeholder="请输入模板名称" v-model.trim="queryParams.tableName"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-row justify="center">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>

    <el-row>
      <p class="pinformation" style="margin-top: 8.5px">原始记录列表</p>
      <p class="totalStyle" style="margin-top: 6px">共 {{ total }} 条数据</p>
    </el-row>

    <el-table ref="mainTableRef" :data="recordList" v-loading="loading">
      <el-table-column show-overflow-tooltip label="方法编号" align="center" width="200" prop="methodCode" />
      <el-table-column show-overflow-tooltip label="方法名称" width="300" align="center">
            <template #default="scope">
              <span v-html="proxy.getSupAndSub(scope.row.methodName)"></span>
            </template>
          </el-table-column>
      <el-table-column show-overflow-tooltip label="模板名称" align="center" prop="tableName">
        <template #default="scope">
          <span v-if="scope.row.tableName">{{ scope.row.tableName }}</span>
          <span v-else style="color: red">未找到关联的模板</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="检测项目" align="center">
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.jcxmNames)"></span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="样品编号数" align="center" width="100" prop="sampleCodeCount" />
      <el-table-column show-overflow-tooltip label="用户组" align="center" prop="userGroupName" width="150"
        v-if="props.iden == 1" />
      <el-table-column show-overflow-tooltip label="填报时间" align="center" prop="writeTime" v-if="props.iden == 2" />
      <el-table-column show-overflow-tooltip label="创建时间" align="center" prop="updateTime" />
      <el-table-column show-overflow-tooltip label="状态" align="center" prop="statusName" width="100"
        v-if="props.iden == 2">
        <template #default="scope">
          待填报
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="105">
        <template #default="scope">
          <el-button link type="success" @click="handleAdd(scope.row)" v-if="!scope.row.parentId">选择样品</el-button>
          <el-button link type="success" @click="handleEdit(scope.row)" v-else>数据填报</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup>
import { listLabMethod, listLabMethod1 } from "@/api/lab/labMethod";
const { proxy } = getCurrentInstance();

const queryParams = ref({ pageNum: 1, pageSize: 10, status: 1 });
const total = ref(0);
const loading = ref(false);
const recordList = ref([]);
const dateRange = ref([]);
const statusList = ref([
  { value: 1, label: "待填报" },
  { value: 2, label: "已填报" },
]);

const props = defineProps({
  iden: {
    type: Number,
  },
});

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}
function getList() {
  loading.value = true;
  (props.iden == 1 ? listLabMethod : listLabMethod1)(
    proxy.addDateRange(queryParams.value, dateRange.value)
  ).then((res) => {
    loading.value = false;
    recordList.value = res.rows;
    total.value = res.total;
  });
}

getList();

const emit = defineEmits(["add"]["edit"]);

function handleAdd(row) {
  emit("add", row);
}

function handleEdit(row) {
  emit("edit", row);
}

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

defineExpose({
  getList,
});
</script>
